package br.com.sgpv2.hibernate.secretaria;

import java.util.Collections;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import br.com.sgpv2.entity.organograma.Secretaria;
import br.com.sgpv2.entity.pessoa.Contrato;
import br.com.sgpv2.hibernate.infra.SGPGenericDAO;

@Repository(value = "secretariaRepository")
public class SecretariaHibernateDAO extends SGPGenericDAO<Secretaria> implements SecretariaRepository{


	/**
	 * Construtor padrão.
	 * 
	 */
	@Autowired
	public SecretariaHibernateDAO(
			@Qualifier("sessionFactory") SessionFactory factory) {
		super(Secretaria.class);
		super.setSessionFactory(factory);
	}

	//TODO alterar método desse DAO e colocar em Contratos
	@Override
	public List<Contrato> listarContratosPorSecretaria(int idSecretaria) {
		Criteria criteria = createCriteria(Contrato.class);
		criteria.createAlias("secretaria", "s");
		criteria.add(Restrictions.eq("s.id", idSecretaria));

		return Collections.checkedList((List<Contrato>) criteria.list(),
				Contrato.class);
	}
	
	public List<Secretaria> listarOrder(String atributoOrder) {
		Criteria criteria = createCriteria(Secretaria.class);
		criteria.addOrder(Order.asc(atributoOrder));
		return Collections.checkedList((List<Secretaria>) criteria.list(),
				Secretaria.class);
	}
	
	public List<Secretaria> consultarPorDescricao(String descricao) {
		Criteria criteria = createCriteria(Secretaria.class);
		criteria.add(Restrictions.like("descricao", descricao, MatchMode.ANYWHERE));
		criteria.addOrder(Order.asc("descricao"));
		return Collections.checkedList((List<Secretaria>) criteria.list(),
				Secretaria.class);
	}

}
